import uni_request from './uni_request.js'
const request = uni_request({
	timeout: 10000,
	// #ifdef H5
	// baseURL: `/apiv1`,
	// baseURL: `http://192.168.59.102:5389`,
	// baseURL: `http://192.168.60.238:5389`,	
	// baseURL: "",
	// #endif
	// baseURL: `http://192.168.59.102:5389`,
	// #ifdef APP-PLUS
	// baseURL: "http://192.168.3.46:5389",
	// #endif	
})

request.interceptors.request.use(async config => { // 请求拦截器(局部)
	// console.log("请求",config);
	config.header['token'] = uni.getStorageSync('token');
	config.header['Content-Type'] = 'application/json; charset=UTF-8';
	return config
})

request.interceptors.response.use((response, ...args) => { // 响应拦截器
	// console.log("响应",response);
	if (response.statusCode === 200) { //成功

	} else if (response.statusCode === 401) {
		// 移出本地存储
		uni.removeStorageSync("userinfo");
		uni.removeStorageSync("token");
		uni.reLaunch({
			url: "/pages/login/login"
		})
	} else { // 服务器响应不为 20000 的统一处理方法
		uni.showToast({
			title: '服务器响应异常',
			icon: 'none'
		})
	}
	return response
})

// request.interceptors.response.use(async (response, ...args) => { // 响应拦截器
// 	if (response.data.code == 401) {
// 		uni.redirectTo({
// 			url: "../pages/login/login"
// 		})
// 	}
// 	return response
// })

request.onerror = async (...args) => { // 请求失败统一处理方法
	console.log('onerror', args)
}

export default request